/******************************************************************************
* Copyright (C) 2016, Huada Semiconductor Co.,Ltd All rights reserved.
*
* This software is owned and published by:
* Huada Semiconductor Co.,Ltd ("HDSC").
*
* BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
* BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
*
* This software contains source code for use with HDSC
* components. This software is licensed by HDSC to be adapted only
* for use in systems utilizing HDSC components. HDSC shall not be
* responsible for misuse or illegal use of this software for devices not
* supported herein. HDSC is providing this software "AS IS" and will
* not be responsible for issues arising from incorrect user implementation
* of the software.
*
* Disclaimer:
* HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
* REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
* ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
* WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
* WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
* WARRANTY OF NONINFRINGEMENT.
* HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
* NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
* LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
* LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
* INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
* SAVINGS OR PROFITS,
* EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
* INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
* FROM, THE SOFTWARE.
*
* This software may be replicated in part or whole for the licensed use,
* with the restriction that this Disclaimer and Copyright notice must be
* included with each copy of this software, whether used in part or whole,
* at all times.
*/
/******************************************************************************/
/** \file main.c
 **
 ** A detailed description is available at
 ** @link Sample Group Some description @endlink
 **
 **   - 2016-02-16  1.0  XYZ First version for Device Driver Library of Module.
 **
 ******************************************************************************/

/******************************************************************************
 * Include files
 ******************************************************************************/

#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "queue.h"
#include "semphr.h"
#include "event_groups.h"
#include "FreeRTOSConfig.h"

#include "gpio.h"
/******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/

/******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/

/******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/

/******************************************************************************
 * Local variable definitions ('static')                                                   *
 ******************************************************************************/

/******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/

/*****************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/
 xQueueHandle xQueue; /*声明一个类型为 xQueueHandle 的变量. 其用于保存队列句柄，以便在任务中可以引用此队列*/
 
 static void Gpio_Config(void);
 
 void vTask1(void *pvParameters)
 {
	long rDelay = 0;
	portBASE_TYPE xStatus;
	 
    while (1)
    {
		xStatus = xQueueReceive(xQueue, &rDelay, 0);
		if (xStatus == pdPASS)
		{
			/*收到任务2发来的关闭led命令，关闭led指定时间*/
			Gpio_ClrIO(GpioPortD, GpioPin5); 
			vTaskDelay((1000*rDelay)/portTICK_RATE_MS);
		}else
		{
			/* 输出高电平，LED3灭 */
			Gpio_ClrIO(GpioPortD, GpioPin5); 
			/* 延时500ms，延时期间任务进入阻塞态 */
			vTaskDelay(500/portTICK_RATE_MS);
			/* 输出低电平，LED3亮*/
			Gpio_SetIO(GpioPortD, GpioPin5);
			/* 延时500ms，延时期间任务进入阻塞态 */
			vTaskDelay(500/portTICK_RATE_MS); 
		}
    }
 }
 
 void vTask2(void *pvParameters)
 {
	long tDelay = 3;
	 
    while (1)
    {
        while (FALSE == Gpio_GetInputIO(GpioPortD, GpioPin4))  /* 按键按下 */
        {
            /* 发送消息队列，通知任务1关闭led，关闭时间tDelay设定，单位s */
			xQueueSendToBack(xQueue, &tDelay, 0);
            /* 延时500ms，防止按键按下过程中，多次执行此段代码，延时期间任务进入阻塞态 */
			vTaskDelay(500/portTICK_RATE_MS);
        }
    }
 }
/**
 ******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 ** This sample
 **
 ******************************************************************************/
int32_t main(void)
{
	Gpio_Config();
	
	/* 创建的队列用于保存最多5个值，每个数据单元都有足够的空间来存储一个uint_32型变量 */
	xQueue = xQueueCreate(5, sizeof(long));
	if(xQueue != NULL)
    {
		/* 创建任务1，控制led灯 */
		xTaskCreate(vTask1, "Task1", 100, NULL, 1, NULL);
		/* 创建任务2，检测按键 */
		xTaskCreate(vTask2, "Task2", 100, NULL, 1, NULL);
		/* 启动调度器，任务开始执行 */
		vTaskStartScheduler();
    }else
	{
		; /* 队列创建失败 */
	}
	while(1);
}

/* CPU IO口配置（包含LED灯和按键的配置）*/
static void Gpio_Config(void)
{
    stc_gpio_config_t stcGpioCfg;
	
    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);     /* 打开GPIO外设时钟门控 */
    
    stcGpioCfg.enDir = GpioDirIn;                               /* 端口方向配置->输入 */    
    stcGpioCfg.enDrv = GpioDrvL;                                /* 端口驱动能力配置->高驱动能力 */
    stcGpioCfg.enPuPd = GpioPu;                                 /* 端口上下拉配置->上拉 */
    stcGpioCfg.enOD = GpioOdDisable;                            /* 端口开漏输出配置->开漏输出关闭 */
    stcGpioCfg.enCtrlMode = GpioAHB;                            /* 端口输入/输出值寄存器总线控制模式配置->AHB */
    
    Gpio_Init(GpioPortD, GpioPin4, &stcGpioCfg);                /* GPIO IO PD04初始化(PD04在STK上外接KEY(USER)) */
    
    stcGpioCfg.enDir = GpioDirOut;                              /* 端口方向配置->输出(其它参数与以上（输入）配置参数一致) */
    stcGpioCfg.enPuPd = GpioPd;                                 /* 端口上下拉配置->下拉 */

    Gpio_Init(GpioPortD, GpioPin5, &stcGpioCfg);                /* GPIO IO PD05初始化(PD05在STK上外接LED(LED3)) */
}
/******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/


